package com.csmz.mybookreadernew.UI.Fragment;

import android.os.Handler;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.csmz.mybookreadernew.Adapter.FragmentAdapter;
import com.csmz.mybookreadernew.Host.RssHost;
import com.csmz.mybookreadernew.R;
import com.csmz.mybookreadernew.RssUtils.RssFeed;
import com.csmz.mybookreadernew.RssUtils.RssFeed_SAXParser;
import com.csmz.mybookreadernew.UI.Fragment.MyVideo.MyNewsVideoFragment_00;
import com.csmz.mybookreadernew.UI.Fragment.MyVideo.MyNewsVideoFragment_01;
import com.csmz.mybookreadernew.UI.Fragment.MyVideo.MyNewsVideoFragment_02;
import com.csmz.mybookreadernew.UI.Fragment.MyVideo.MyNewsVideoFragment_03;
import com.csmz.mybookreadernew.UI.Fragment.MyVideo.MyNewsVideoFragment_04;
import com.csmz.mybookreadernew.UI.Fragment.MyVideo.MyNewsVideoFragment_05;
import com.csmz.mybookreadernew.UI.Fragment.MyVideo.MyNewsVideoFragment_06;
import com.csmz.mybookreadernew.UI.Fragment.MyVideo.MyNewsVideoFragment_07;
import com.csmz.mybookreadernew.UI.Fragment.MyVideo.MyNewsVideoFragment_08;
import com.csmz.mybookreadernew.UI.Fragment.MyVideo.MyNewsVideoFragment_09;
import com.king.base.BaseFragment;

import org.xml.sax.SAXException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

public class MyVideoFragment extends BaseFragment {

    private ViewPager viewPager;
    private TabLayout tabLayout;
    private FragmentAdapter adapter;
    private List<Fragment> fragments;

    private RssFeed feed01=null;
    private RssFeed feed02=null;
    private RssFeed feed03=null;
    private RssFeed feed04=null;
    private RssFeed feed05=null;
    private RssFeed feed06=null;
    private RssFeed feed07=null;
    private RssFeed feed08=null;
    private RssFeed feed09=null;
    private RssFeed feed10=null;

    private Handler uiHandler=new Handler();
    private DownLoadThread downLoadThread=new DownLoadThread();

    private MyNewsVideoFragment_00 myNewsVideoFragment_00;
    private MyNewsVideoFragment_01 myNewsVideoFragment_01;
    private MyNewsVideoFragment_02 myNewsVideoFragment_02;
    private MyNewsVideoFragment_03 myNewsVideoFragment_03;
    private MyNewsVideoFragment_04 myNewsVideoFragment_04;
    private MyNewsVideoFragment_05 myNewsVideoFragment_05;
    private MyNewsVideoFragment_06 myNewsVideoFragment_06;
    private MyNewsVideoFragment_07 myNewsVideoFragment_07;
    private MyNewsVideoFragment_08 myNewsVideoFragment_08;
    private MyNewsVideoFragment_09 myNewsVideoFragment_09;

    private boolean start=false;



    @Override
    public int inflaterRootView() {
        return R.layout.content_main;
    }

    @Override
    public void initUI() {
        if (!start){
            downLoadThread.start();
        }
        initView();
    }
    class DownLoadThread extends Thread{
        @Override
        public void run() {
            start=true;
            try {
                feed01 = new RssFeed_SAXParser().getFeed(RssHost.RSS_URL_TX_SPXW);
                feed02= new RssFeed_SAXParser().getFeed(RssHost.RSS_URL_TX_CJSP);
                feed03= new RssFeed_SAXParser().getFeed(RssHost.RSS_URL_TX_YLSP);
                feed04= new RssFeed_SAXParser().getFeed(RssHost.RSS_URL_TX_TYSP);
                feed05= new RssFeed_SAXParser().getFeed(RssHost.RSS_URL_TX_QCPD);
                feed06= new RssFeed_SAXParser().getFeed(RssHost.RSS_URL_TX_DCSP);
                feed07= new RssFeed_SAXParser().getFeed(RssHost.RSS_URL_TX_DQXW);
                feed08= new RssFeed_SAXParser().getFeed(RssHost.RSS_URL_TX_GDXW);
                feed09= new RssFeed_SAXParser().getFeed(RssHost.RSS_URL_TX_DCXW);
                feed10= new RssFeed_SAXParser().getFeed(RssHost.RSS_URL_TX_DYXW);
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Runnable runnable=new Runnable() {
                @Override
                public void run() {
                    MyNewsVideoFragment_00.getRssFeed(getActivity(),feed01);
                    MyNewsVideoFragment_01.getRssFeed(getActivity(),feed02);
                    MyNewsVideoFragment_02.getRssFeed(getActivity(),feed03);
                    MyNewsVideoFragment_03.getRssFeed(getActivity(),feed04);
                    MyNewsVideoFragment_04.getRssFeed(getActivity(),feed05);
                    MyNewsVideoFragment_05.getRssFeed(getActivity(),feed06);
                    MyNewsVideoFragment_06.getRssFeed(getActivity(),feed07);
                    MyNewsVideoFragment_07.getRssFeed(getActivity(),feed08);
                    MyNewsVideoFragment_08.getRssFeed(getActivity(),feed09);
                    MyNewsVideoFragment_09.getRssFeed(getActivity(),feed10);
                }
            };
            uiHandler.post(runnable);
        }
    }

    private void initView() {
        viewPager=findView(R.id.main_viewpager);
        tabLayout=findView(R.id.tab_layout);
        fragments = new ArrayList<>();
        myNewsVideoFragment_00 = new MyNewsVideoFragment_00();
        myNewsVideoFragment_01 = new MyNewsVideoFragment_01();
        myNewsVideoFragment_02 = new MyNewsVideoFragment_02();
        myNewsVideoFragment_03 = new MyNewsVideoFragment_03();
        myNewsVideoFragment_04 = new MyNewsVideoFragment_04();
        myNewsVideoFragment_05 = new MyNewsVideoFragment_05();
        myNewsVideoFragment_06 = new MyNewsVideoFragment_06();
        myNewsVideoFragment_07 = new MyNewsVideoFragment_07();
        myNewsVideoFragment_08 = new MyNewsVideoFragment_08();
        myNewsVideoFragment_09 = new MyNewsVideoFragment_09();
        fragments.add(myNewsVideoFragment_00);
        fragments.add(myNewsVideoFragment_01);
        fragments.add(myNewsVideoFragment_02);
        fragments.add(myNewsVideoFragment_03);
        fragments.add(myNewsVideoFragment_04);
        fragments.add(myNewsVideoFragment_05);
        fragments.add(myNewsVideoFragment_06);
        fragments.add(myNewsVideoFragment_07);
        fragments.add(myNewsVideoFragment_08);
        fragments.add(myNewsVideoFragment_09);
        adapter = new FragmentAdapter(getChildFragmentManager(),fragments);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.getTabAt(0).setText("视频新闻");
        tabLayout.getTabAt(1).setText("财经视频");
        tabLayout.getTabAt(2).setText("娱乐视频");
        tabLayout.getTabAt(3).setText("体育视频");
        tabLayout.getTabAt(4).setText("汽车行情");
        tabLayout.getTabAt(5).setText("大成视频");
        tabLayout.getTabAt(6).setText("大秦新闻");
        tabLayout.getTabAt(7).setText("大粤新闻");
        tabLayout.getTabAt(8).setText("大楚新闻");
        tabLayout.getTabAt(9).setText("大渝新闻");
    }

    @Override
    public void initData() {

    }

    @Override
    public void addListeners() {

    }
}
